package com.sys.user.service;

import com.sys.core.exception.ClassCheckException;
import com.sys.user.base.service.BaseService;
import com.sys.user.domain.Xyuserrole;
import com.sys.user.persistence.XyuserroleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户角色关联
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class XyuserroleService extends BaseService<Xyuserrole> {
    @Autowired
    private XyuserroleMapper xyuserroleMapper;

    @Autowired
    public void setBaseMapper(XyuserroleMapper xyuserroleMapper) {
        this.baseMapper = xyuserroleMapper;
    }


    public void checkEntity(Xyuserrole xyuserrole) throws ClassCheckException {
        if(xyuserrole.getUserroleId()!=null&&xyuserrole.getUserroleId().length()>19){
            throw new ClassCheckException("用户角色关联不能超过19个字符");
        }
        if(xyuserrole.getUserId()!=null&&xyuserrole.getUserId().length()>19){
            throw new ClassCheckException("用户角色关联不能超过19个字符");
        }
        if(xyuserrole.getRoleId()!=null&&xyuserrole.getRoleId().length()>19){
            throw new ClassCheckException("用户角色关联不能超过19个字符");
        }
    }
    public void checkListEntity(List<Xyuserrole> listXyuserrole) throws ClassCheckException {

        for (Xyuserrole xyuserrole : listXyuserrole) {
            checkEntity(xyuserrole);
        }

    }


    public List<Xyuserrole> queryByRoleId(String roleId) {
        return xyuserroleMapper.queryByRoleId(roleId);
    }

    public void deleteByEntityIds(List<Xyuserrole> xyuserroleList) {
        List<String> listId = new ArrayList<String>();
        for (Xyuserrole xyuserrole : xyuserroleList) {
            listId.add(xyuserrole.getUserroleId());
        }
        deleteByIds(listId);
    }

    public List<Xyuserrole> queryByUserId(String userId) {
        return  xyuserroleMapper.queryByUserId(userId);
    }
}
